package top.cflwork.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import top.cflwork.vo.ShopVo;
import top.cflwork.service.ShopService;
import top.cflwork.controller.BaseController;
import top.cflwork.util.PageUtils;
import top.cflwork.util.Query;
import top.cflwork.util.R;

/**
 * 旧物表
 * 
 * @author cfl
 * @email 275300091@qq.com
 * @date 2020-04-21 13:59:41
 */
 
@Controller
@RequestMapping("/shop")
public class ShopController extends BaseController{
	@Autowired
	private ShopService shopService;
	
	@GetMapping("shopPage")
	@RequiresPermissions("shop:shopPage")
	public String Shop(){
	    return "/shop/shop";
	}

	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("shop:list")
	public PageUtils list(ShopVo shopVo){
		shopVo.setAdminId(getUserId());
		//查询列表数据
		List<ShopVo> shopList = shopService.list(shopVo);
		Long total = shopService.count(shopVo);
		PageUtils pageUtils = new PageUtils(shopList, total);
		return pageUtils;
	}

    @ResponseBody
    @PostMapping("/listAll")
    @RequiresPermissions("shop:list")
    public List<ShopVo> listAll(ShopVo shopVo){
		shopVo.setAdminId(getUserId());
        //查询列表数据
        List<ShopVo> shopList = shopService.list(shopVo);
        return shopList;
    }

	@GetMapping("/add")
	@RequiresPermissions("shop:add")
	public String add(){
	    return "/shop/add";
	}

	/*@GetMapping("/edit/{id}")
	@RequiresPermissions("shop:edit")
	public String edit(@PathVariable("id") String id,Model model){
		ShopVo shop = shopService.get(id);
		model.addAttribute("shop", shop);
	    return "/shop/edit";
	}*/

    @GetMapping("/edit/{id}")
    @RequiresPermissions("shop:edit")
    @ResponseBody
    public ShopVo edit(@PathVariable("id") String id){
			ShopVo shop = shopService.get(id);
        return shop;
    }

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:add")
	public R save( ShopVo shop){
		shop.setAdminId(getUserId());
		shop.setAdminName(getUsername());
		shop.setStatus(0);
		if(shopService.save(shop)>0){
			return R.ok("新增成功");
		}
		return R.error("新增失败");
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:update")
	public R update( ShopVo shop){
		shopService.update(shop);
		return R.ok("修改成功");
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:remove")
	public R remove( String id){
		if(shopService.remove(id)>0){
		return R.ok("删除成功");
		}
		return R.error("删除失败");
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		shopService.batchRemove(ids);
		return R.ok("批量删除成功");
	}
    /**
     * 删除
     */
    @PostMapping( "/batchSave")
    @ResponseBody
    @RequiresPermissions("shop:batchSave")
    public R batchSave(List<ShopVo> shopList){
			shopService.batchSave(shopList);
        return R.ok("批量新增成功");
    }

	@ResponseBody
	@GetMapping("/shopListAll")
	public List<ShopVo> shopListAll(){
		List<ShopVo> shopList = shopService.shopListAll();
		return shopList;
	}

}
